<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: tool_database.tpl.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
	defined('IN_TWCMS') || exit('Access Denied');
	include load_tpl('head');
?>

	<div class="head">
		<dl>
			<dd class="on">数据库备份</dd>
			<dd>数据库还原</dd>
			<dd>数据库信息</dd>
		</dl>
	</div>

	<div class="p c2">
		<div class="cc">
			<form id="backup" action="<?=$this->path?>&do=sqlbackup" method="get">
				<table class="tb">
					<tr><th>分卷备份大小</th><td><input id="size" name="size" type="text" class="inp" maxlength="2" value="2" onkeyup="this.value=this.value.replace(/\D/g,'')" style="width:30px"> M</td></tr>
				</table>
				<div class="tb_b"><input type="submit" value="开始" class="but1"></div>
			</form>
		</div>
		<div class="cc un"></div>
		<div class="cc un"></div>
	</div>
<script type="text/javascript">
//对话框
function addDialog(title) {
	if($(".ajaxoverlay").length < 1) {
		$("body").prepend('<div class="ajaxoverlay"></div><div class="ajaxtips"><div class="ajaxbox btrue" style="width:400px"><div class="bkprompt"><b>'+title+': <font id="backupmsg">准备中...</font></b></div><div class="pbr"><div class="pbr-within" style="width:0%"></div></div></div></div>');
		twAjax.setTopLeft();
	}
}

//数据库还原Tab点击时执行
$(".head dl dd:eq(1)").click(load_filelist);
function load_filelist() {
	if($(".head dl dd:eq(1)").attr("load") != "yes") {
		twAjax.loading();
		twAjax.get("?mod=tool&action=database&do=ajax_filelist&r="+time(), function(data){
			$(".p .cc:eq(1)").html(data);
			twAjax.remove();
			$(".head dl dd:eq(1)").attr("load","yes");
			load_sqllistSet();
		});
	}
}

//数据库信息Tab点击时执行
$(".head dl dd:eq(2)").click(function(){
	if($(this).attr("load") != "yes") {
		twAjax.loading();
		twAjax.get("?mod=tool&action=database&do=ajax_tablelist&r="+time(), function(data){
			$(".p .cc:eq(2)").html(data);
			twAjax.remove();
			$(".head dl dd:eq(2)").attr("load","yes");
			load_tableSet();
		});
	}
});

//载入数据库还原批量操作功能
function load_sqllistSet() {
	$("#check_all").change(function(){
		var CHK = $(this).attr("checked"), obj = $("input[name='chk_name']");
		if(CHK == "checked") {
			obj.attr("checked", CHK);
		}else{
			obj.removeAttr("checked");
		}
		checkBatchDel();
	});

	$("input[name='chk_name']").change(function(){
		var CHK = $(this).attr("checked")
		if(CHK == "checked") {
			$(this).attr("checked", CHK);
		}else{
			$(this).removeAttr("checked");
		}
		checkBatchDel();
	});

	//还原
	$(".sqlrev,.sqldel").attr("href","javascript:;");
	$(".sqlrev").click(function(){
		var sqlname = $(this).parent().parent().find("td:eq(2)").html();
		var volumes = $(this).parent().parent().find("td:last").html();
		twAjax.confirm("将覆盖当前数据库，是否恢复？", function(){
			addDialog("分卷恢复");
			doFor("?mod=tool&action=database&do=ajax_revert&sqlname="+sqlname+"&volumes="+volumes);
		});
	});

	//删除
	$(".sqldel").click(function(){
		var obj = $(this).parent().parent();
		twAjax.confirm("删除不可恢复，确定删除？", function(){
			twAjax.postd("?mod=tool&action=database&do=ajax_del", {"sqlname":obj.find("td:eq(2)").html()}, function(data){
				twAjax.alert(data);
				if(window.twD.err==0) {
					obj.remove();
					$("#check_all").removeAttr("checked");
					checkBatchDel();
				}
			});
		});
	});
}

//备份
$("#backup").submit(function(){
	addDialog("分卷备份");
	doFor($(this).attr("action")+"&size="+$("#size").val(), true);
	return false;
});

//检查是否激活批量删除
function checkBatchDel() {
	var Len = $("input[name='chk_name'][checked='checked']").length;
	if(Len > 0) {
		if($(".sqlbatch").length == 0) $(".head dl").append('<input type="button" value="批量删除" onclick="batchDel()" class="but1 sqlbatch">');
	}else{
		$(".sqlbatch").remove();
	}
}

//批量删除
function batchDel() {
	var arr = new Array(), obj = $("input[name='chk_name'][checked='checked']");
	obj.each(function(i) {
		arr[i] = $(this).parent().parent().find("td:eq(2)").html();
	});
	twAjax.confirm("删除不可恢复，确定删除？", function(){
		twAjax.postd("?mod=tool&action=database&do=batch_del", {dirarr:arr}, function(data){
			twAjax.alert(data);
			if(window.twD.err==0) {obj.parent().parent().remove(); $("#check_all").removeAttr("checked"); checkBatchDel();}
		});
	});
}

//循环执行
function doFor(url, isbackup) {
	twAjax.get(url+"&r="+time(), function(data){
		var twD = toJson(data);
		twD.pbr += "%";
		$("#backupmsg").html(twD.msg);
		if(twD.status == 0) {
			$("#backupmsg").css("color","green");
			$(".pbr-within").width(twD.pbr).html(twD.pbr);
			$(".bkprompt").prepend('<a id="bkClose" class="but3" href="javascript:;">关闭</a>'+(isbackup ? '<a id="bkView" class="but3" href="javascript:;">查看</a>' : ''));
			$("#bkClose").click(function(){$(".ajaxoverlay,.ajaxtips").remove();});
			if(isbackup) {
				$(".head dl dd:eq(1)").removeAttr("load");
				$("#bkView").click(function(){
					$(".ajaxoverlay,.ajaxtips").remove();
					$(".head dl dd").removeAttr("class");
					$(".p .cc").hide();
					$(".head dl dd:eq(1)").addClass("on");
					$(".p .cc").eq(1).show();
					load_filelist();
				});
			}
		}else if(twD.status == 1) {
			$("#backupmsg").css("color","green");
			$(".pbr-within").width(twD.pbr).html(twD.pbr);
			doFor(twD.url, isbackup);
		}else if(twD.status == 2) {
			$("#backupmsg").css("color","red");
			$(".bkprompt").prepend('<a id="bkClose" class="but3" href="javascript:;">关闭</a>');
			$("#bkClose").click(function(){$(".ajaxoverlay,.ajaxtips").remove();});
		}
	});
}

//载入数据表操作功能
function load_tableSet() {
	$(".table_all").change(function(){
		var CHK = $(this).attr("checked"), obj = $(this).parent().parent().parent().find("input[name='chk_table']");
		if(CHK == "checked") {
			obj.attr("checked", CHK);
		}else{
			obj.removeAttr("checked");
		}
		checkBatchTable();
	});

	$("input[name='chk_table']").change(function(){
		var CHK = $(this).attr("checked")
		if(CHK == "checked") {
			$(this).attr("checked", CHK);
		}else{
			$(this).removeAttr("checked");
		}
		checkBatchTable();
	});
}

//检查是否激活批量操作数据表
function checkBatchTable() {
	var len = $("input[name='chk_table'][checked='checked']").length;
	if(len > 0) {
		if($(".tabbatch").length == 0) $(".head dl").append('<input type="button" value="优化" onclick="tableBatch(\'optimize\')" class="but1 tabbatch"><input type="button" value="修复" onclick="tableBatch(\'repair\')" class="but1 tabbatch">');
	}else{
		$(".tabbatch").remove();
		$(".table_all").removeAttr("checked");
	}
}

//批量优化/修复功能
function tableBatch(type) {
	var arr = new Array(), obj = $("input[name='chk_table'][checked='checked']");
	obj.each(function(i) {
		arr[i] = $(this).parent().parent().find("td:eq(1)").html();
	});
	twAjax.postd("?mod=tool&action=database&do=batch_"+type, {"batcharr":arr}, function(data){
		twAjax.alert(data);
	});
}

//改变Tab时清空选择功能
$(".head dl dd[class!='on']").click(function(){
	$("#check_all,input[name='chk_name'],input[name='chk_table']").removeAttr("checked");
	checkBatchDel();
	checkBatchTable();
});
</script>
</body>
</html>